package pri.damai.xiaowu.common.core.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用分页结果返回类
 * @Desc
 * @Author DaMai
 * @Date 2021/5/6 15:07
 * 但行好事，莫问前程。
 */
@Data
@Schema
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class QueryResultCommonVO<T> {

    @Schema(description = "当前页")
    private Long pageNum;
    @Schema(description = "每页条数")
    private Long pageSize;
    @Schema(description = "总页数")
    private Long totalPages;
    @Schema(description = "总条数")
    private Long totalElements;
    @Schema(description = "分页内容")
    private List<T> content;

    /**
     * 创建空结果
     * @apiNote [附加描述]
     * @return com.ruiruan.pre.common.core.vo.QueryResultCommonVO<java.lang.Object>
     * @author DaMai
     * @date 2021/5/6 15:10
     */
    public static <T> QueryResultCommonVO<T> buildEmpty() {
        QueryResultCommonVO<T> result = new QueryResultCommonVO<>();
        result.setContent(new ArrayList<T>());
        result.setPageNum(1L);
        result.setPageSize(1L);
        result.setTotalElements(0L);
        result.setTotalPages(1L);
        return result;
    }

    /**
     * 构建分页结果
     * @apiNote [附加描述]
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @param totalPages 总页数
     * @param totalElements 总数量
     * @param content 当前页
     * @return com.ruiruan.pre.common.core.vo.QueryResultCommonVO<T>
     * @author DaMai
     * @date 2021/5/12 14:14
     */
    public static <T> QueryResultCommonVO<T> build(Long pageNum, Long pageSize, Long totalPages, Long totalElements, List<T> content) {
        QueryResultCommonVO<T> result = new QueryResultCommonVO<>();
        result.setContent(content)
                .setPageNum(pageNum)
                .setPageSize(pageSize)
                .setTotalElements(totalElements)
                .setTotalPages(totalPages);
        return result;
    }

    /**
     * 构建分页结果
     * @apiNote [附加描述]
     * @param page mybatis-plus 的分页结果抽象接口
     * @param content 自定义的分页内容
     * @return com.ruiruan.pre.common.core.vo.QueryResultCommonVO<T>
     * @author DaMai
     * @date 2021/5/12 14:01
     */
    public static <T> QueryResultCommonVO<T> buildByIPageAndContent(IPage<?> page, List<T> content) {
        QueryResultCommonVO<T> result = new QueryResultCommonVO<>();
        result.setContent(content)
                .setPageNum(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalElements(page.getTotal())
                .setTotalPages(page.getPages());
        return result;
    }

    /**
     * 构建分页结果
     * @apiNote [附加描述]
     * @param page mybatis-plus 的分页结果抽象接口
     * @return com.ruiruan.pre.common.core.vo.QueryResultCommonVO<T>
     * @author DaMai
     * @date 2021/5/12 14:03
     */
    public static <T> QueryResultCommonVO<T> buildByIPage(IPage<T> page) {
        QueryResultCommonVO<T> result = new QueryResultCommonVO<>();
        result.setContent(page.getRecords())
                .setPageNum(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalElements(page.getTotal())
                .setTotalPages(page.getPages());
        return result;
    }
}
